Skip to content

Method: computeNextMove(int, TicTacToePlayer, TicTacToeState)

1: package de.fhdw.gaming.ipspiel23.tictactoe.strategy.calculator;
2:
3: import java.util.Optional;
4:
5: import de.fhdw.gaming.core.domain.GameException;
6: import de.fhdw.gaming.ipspiel23.gst.domain.ICalculatorKopplung;
7: import de.fhdw.gaming.ipspiel23.gst.domain.IKopplung;
8: import de.fhdw.gaming.ipspiel23.gst.domain.impl.GstKopplungsMoveCalculator;
9: import de.fhdw.gaming.ipspiel23.gst.strategies.impl.GstKopplungNegaMax;
10: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToePlayer;
11: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeState;
12: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.TicTacToeStrategy;
13: import de.fhdw.gaming.ipspiel23.tictactoe.core.domain.impl.TicTacToeKopplungImpl;
14: import de.fhdw.gaming.ipspiel23.tictactoe.core.moves.TicTacToeMove;
15:
16: /**
17: * Implementation of the {@link TicTacToeStrategy} interface.
18: * It computes the next move by using the GstKopplungNegaMax algorithms.
19: * This strategy is specifically designed for Tic-Tac-Toe games.
20: *
21: * The calculation is performed by an instance of {@link ICalculatorKopplung}
22: * with a {@link GstKopplungNegaMax} algorithm.
23: * The game state evaluation is done using an instance of {@link TicTacToeKopplungImpl}.
24: *
25: * @author borkowitz
26: */
27: public class TicTacToeCalculatorMoveStrategy implements TicTacToeStrategy {
28:
29: /**
30: * The calculator used for computing the next move.
31: */
32: private final ICalculatorKopplung<TicTacToePlayer, TicTacToeState> calc;
33:
34: /**
35: * The game state coupling used for evaluation and move calculation.
36: */
37: private final IKopplung<TicTacToePlayer, TicTacToeState> ticTacToeKopplung;
38:
39: /**
40: * new TicTacToeCalculatorMoveStrategy Constructor.
41: */
42: public TicTacToeCalculatorMoveStrategy() {
43: this.calc = new GstKopplungsMoveCalculator<>();
44: this.ticTacToeKopplung = new TicTacToeKopplungImpl();
45: }
46:
47:
48: /**
49: * Computes the next move for the given Tic-Tac-Toe game State.
50: *
51: * @param gameId The ID of the game.
52: * @param player The current player.
53: * @param state The current game state.
54: * @return An optional containing the next Tic-Tac-Toe move.
55: * @throws GameException If an error occurs during the game.
56: * @throws InterruptedException If the calculation is interrupted.
57: */
58: @Override
59: public Optional<TicTacToeMove> computeNextMove(final int gameId, final TicTacToePlayer player,
60: final TicTacToeState state) throws GameException, InterruptedException {
61: return Optional.of((TicTacToeMove) calc.calculateMove(
62: this.ticTacToeKopplung, state, 500, new GstKopplungNegaMax<>()));
63: }
64:
65: /**
66: * Returns a string representation of this TicTacToeCalculatorMoveStrategy object.
67: *
68: * @return A string representation of the object.
69: */
70: @Override
71: public String toString() {
72: return TicTacToeCalculatorMoveStrategy.class.getSimpleName();
73: }
74: }